package cn.stylefeng.guns.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jj
 * @date 2019/12/9 15:18
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 声明队列
     *
     * @return
     */
    @Bean
    public Queue queueTransaction() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_TRANSACTION, true);
    }


    @Bean
    public Queue queueContract() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_CONTRACT, true);
    }

    @Bean
    public Queue queueQualification() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_QUALIFICATION, true);
    }

    /**
     * 订单超时队列
     *
     * @return
     */
    @Bean
    public Queue queueDisletter() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.XIEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_HANDER);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_DISLETTER).withArguments(args).build();
    }

    /**
     * 订单超时重定向队列
     *
     * @return
     */
    @Bean
    public Queue queueHander() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_HANDER, true);
    }

    /**
     * 订单待确定超时队列
     *
     * @return
     */
    @Bean
    public Queue queueAffirm() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.XIEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_AFFIRMHANDER);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_AFFIRM).withArguments(args).build();
    }
    /**
     * 订单超时重定向队列
     *
     * @return
     */
    @Bean
    public Queue queueAffirmHander() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_AFFIRMHANDER, true);
    }
    /**
     * @description :死信交换机
     * @date : 2019/12/12 17:11
     * @author : jj
     */
    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange(RabbitConstant.XIEXCHANGE);
    }


    /**
     * 声明交互器
     *
     * @return
     */
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange(RabbitConstant.EXCHANGE);
    }


    /**
     * 绑定
     *
     * @return
     */
    @Bean
    public Binding bindingTransaction() {
        return BindingBuilder.bind(queueTransaction()).to(directExchange()).with(RabbitConstant.RK_TRANSACTION);
    }

    /**
     * 绑定
     *
     * @return
     */
    @Bean
    public Binding bindingContract() {
        return BindingBuilder.bind(queueContract()).to(directExchange()).with(RabbitConstant.RK_CONTRACT);
    }

    /**
     * 绑定
     *
     * @return
     */
    @Bean
    public Binding bindingQualification() {
        return BindingBuilder.bind(queueQualification()).to(directExchange()).with(RabbitConstant.RK_QUALIFICATION);
    }

    /**
     * 绑定死信
     *
     * @return
     */
    @Bean
    public Binding bindingQueueDisletter() {
        return BindingBuilder.bind(queueDisletter()).to(delayExchange()).with(RabbitConstant.RK_DISLETTER);
    }

    /**
     * 将重定向队列通过routingKey(TKEY_R)绑定到死信队列的Exchange上
     *
     * @return the binding
     */
    @Bean
    public Binding redirectBinding() {
        return BindingBuilder.bind(queueHander()).to(delayExchange()).with(RabbitConstant.RK_HANDER);
    }

    /**
     * 绑定死信
     *
     * @return
     */
    @Bean
    public Binding bindingQueueAffirm() {
        return BindingBuilder.bind(queueAffirm()).to(delayExchange()).with(RabbitConstant.RK_AFFIRM);
    }

    /**
     * 将重定向队列通过routingKey(TKEY_R)绑定到死信队列的Exchange上
     *
     * @return the binding
     */
    @Bean
    public Binding affirmHanderBinding() {
        return BindingBuilder.bind(queueAffirmHander()).to(delayExchange()).with(RabbitConstant.RK_AFFIRMHANDER);
    }

    /**
     * @description :建仓死信交换机
     * @date : 2019/12/12 17:11
     * @author : jj
     */
    @Bean
    public DirectExchange buildingExchange() {
        return new DirectExchange(RabbitConstant.BUILDEXCHANGE);
    }

    /**
     * 新手建仓队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingXs() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_XS);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_XS).withArguments(args).build();
    }
    /**
     * 新手建仓处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderXs() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_XS, true);
    }
    @Bean
    public Binding BuildingXsBinding() {
        return BindingBuilder.bind(queueBuildingXs()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_XS);
    }
    @Bean
    public Binding BuildingXsHanderBinding() {
        return BindingBuilder.bind(queueBuildinghanderXs()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_XS);
    }

    /**
     * 新手建仓vip队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingXsVip() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_XS_VIP);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_XS_VIP).withArguments(args).build();
    }
    /**
     * 新手建仓vip处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderXsVip() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_XS_VIP, true);
    }
    @Bean
    public Binding BuildingXsVipBinding() {
        return BindingBuilder.bind(queueBuildingXsVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_XS_VIP);
    }
    @Bean
    public Binding BuildingXsVipHanderBinding() {
        return BindingBuilder.bind(queueBuildinghanderXsVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_XS_VIP);
    }


    /**
     * 初级建仓队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingCu() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_CU);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_CU).withArguments(args).build();
    }
    /**
     * 初级建仓处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderCu() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_CU, true);
    }
    @Bean
    public Binding BuildingCuBinding() {
        return BindingBuilder.bind(queueBuildingCu()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_CU);
    }
    @Bean
    public Binding BuildingCuHanderBinding() {
        return BindingBuilder.bind(queueBuildinghanderCu()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_CU);
    }
    /**
     * 初级建仓vip队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingCuVip() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_CU_VIP);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_CU_VIP).withArguments(args).build();
    }
    /**
     * 初级建仓vip处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderCuVip() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_CU_VIP, true);
    }
    @Bean
    public Binding BuildingCuBindingVip() {
        return BindingBuilder.bind(queueBuildingCuVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_CU_VIP);
    }
    @Bean
    public Binding BuildingCuHanderBindingVip() {
        return BindingBuilder.bind(queueBuildinghanderCuVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_CU_VIP);
    }
    /**
     * 中级建仓队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingZh() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_ZH);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_ZH).withArguments(args).build();
    }
    /**
     * 中级建仓处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderZh() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_ZH, true);
    }
    @Bean
    public Binding BuildingZhBinding() {
        return BindingBuilder.bind(queueBuildingZh()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_ZH);
    }
    @Bean
    public Binding BuildingZhHanderBinding() {
        return BindingBuilder.bind(queueBuildinghanderZh()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_ZH);
    }
    /**
     * 中级建仓vip队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingZhVip() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_ZH_VIP);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_ZH_VIP).withArguments(args).build();
    }
    /**
     * 中级建仓vip处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderZhVip() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_ZH_VIP, true);
    }
    @Bean
    public Binding BuildingZhBindingVip() {
        return BindingBuilder.bind(queueBuildingZhVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_ZH_VIP);
    }
    @Bean
    public Binding BuildingZhHanderBindingVip() {
        return BindingBuilder.bind(queueBuildinghanderZhVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_ZH_VIP);
    }
    /**
     * 高级建仓队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingGa() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_GA);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_GA).withArguments(args).build();
    }
    /**
     * 高级建仓处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderGa() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_GA, true);
    }
    @Bean
    public Binding BuildingGaBinding() {
        return BindingBuilder.bind(queueBuildingGa()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_GA);
    }
    @Bean
    public Binding BuildingGaHanderBinding() {
        return BindingBuilder.bind(queueBuildinghanderGa()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_GA);
    }

    /**
     * 高级建仓队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildingGaVip() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    声明  死信交换机
        args.put("x-dead-letter-exchange", RabbitConstant.BUILDEXCHANGE);
//       x-dead-letter-routing-key    声明 死信路由键
        args.put("x-dead-letter-routing-key", RabbitConstant.RK_BUILDINGHANDER_GA_VIP);
        // true表示持久化该队列
        return QueueBuilder.durable(RabbitConstant.QUEUE_BUILDING_GA_VIP).withArguments(args).build();
    }
    /**
     * 高级建仓处理队列
     *
     * @return
     */
    @Bean
    public Queue queueBuildinghanderGaVip() {
        // true表示持久化该队列
        return new Queue(RabbitConstant.QUEUE_BUILDINGHANDER_GA_VIP, true);
    }
    @Bean
    public Binding BuildingGaBindingVip() {
        return BindingBuilder.bind(queueBuildingGaVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDING_GA_VIP);
    }
    @Bean
    public Binding BuildingGaHanderBindingVip() {
        return BindingBuilder.bind(queueBuildinghanderGaVip()).to(buildingExchange()).with(RabbitConstant.RK_BUILDINGHANDER_GA_VIP);
    }
}